<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <mall@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\mall\controller\admin;

use app\mall\base\MallAdminBase;

use app\mall\model\MallSpecCats as MallSpecCatsModel;
use app\mall\model\MallSpecGroups as MallSpecGroupsModel;
use app\mall\model\MallSpecItems as MallSpecItemsModel;

class SpecItems extends MallAdminBase{
    //当前控制器菜单
    public $submenu = [];

    /**
     * TP初始化方法
     */
    public function _initialize($catid = 0) {
        parent::_initialize();
        $this->submenu[] = ['title' => '规格分类列表', 'url' => url('mall/admin.specCats/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '规格组列表', 'url' => url('mall/admin.specGroups/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '规格列表', 'url' => url('mall/admin.specItems/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '更新规格缓存', 'url' => url('mall/admin.specCats/cache'), 'icon' => 'ဂ'];
    }

    /**
     * 后台商品规格值列表
     * @access public
     * @return \think\Response
     */
    public function index($spec_group_id = 0){
        if($spec_group_id){
            $list = MallSpecItemsModel::where('spec_group_id',$spec_group_id)->order('id desc')->paginate(config('mn_admin.admin_pagesize'));
        }else{
            $list = MallSpecItemsModel::order('id desc')->paginate(config('mn_admin.admin_pagesize'));
        }
        $this->assign([
            'submenu'   => $this->submenu,
            'list'      => $list
        ]);
    	return $this->fetch();
    }
    
    /**
     * 后台商品规格添加
     * @access public
     * @return \think\Response
     */
    public function add($spec_group_id = 0){
        $spec_group_id = (int)$spec_group_id;
        if(!$spec_group_id){
            $this->error('缺少父类规格组id');
        }
        $spec_group = MallSpecGroupsModel::get($spec_group_id);
        if(!$spec_group){
            $this->error('父类规格组不存在');
        }
        // POST提交数据
        if($this->request->isPost()){
            $post_data = $this->buildParam();
            // 实例化商品规格模型
            $MallSpecItemsModel = new MallSpecItemsModel;
            // 验证商品规格在当前组是否重复
            $check_repeat = $MallSpecItemsModel->where('spec_group_id',$post_data['spec_group_id'])
                                                ->where('name',$post_data['name'])
                                                ->count();
            if($check_repeat){
                $this->error('商品规格名称重复');
            }
            if($MallSpecItemsModel->allowField(true)->validate(true)->save($post_data)){
                // 更新所有规格缓存
                $MallSpecCatsModel = new MallSpecCatsModel;
                $MallSpecCatsModel->cache();
                $this->success('商品规格添加成功','index');
            }
            else{
                $this->error($MallSpecItemsModel->getError());
            }
        }
        // 获取规格组数组
        $spec_groups = [];
        foreach (MallSpecGroupsModel::all() as $key => $group) {
             $spec_groups[$group->id] = $group->getData();
        }
        $this->assign([
            'submenu'           => $this->submenu,
            'spec_groups'       => $spec_groups,
            'spec_cats'         => MallSpecCatsModel::all(),
            'parent_spec_group' => $spec_group->getData()
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少商品规格id');
        }
        // 实例化商品规格模型
        $spec_item = MallSpecItemsModel::get($id);
        if(!$spec_item){
            $this->error('商品规格不存在','index');
        }
        // 实例化商品规格组模型
        $spec_group = MallSpecGroupsModel::get($spec_item->getData('spec_group_id'));
        if(!$spec_group){
            $this->error('父类规格组不存在');
        }
        // POST提交数据
        if($this->request->isPost()){
            $post_data = $this->buildParam();
            // 验证商品规格在当前组是否重复
            $check_repeat = MallSpecItemsModel::where('id','neq',$id)->where('spec_group_id',$post_data['spec_group_id'])
                                                                        ->where('name',$post_data['name'])
                                                                        ->count();
            if($check_repeat){
                $this->error('商品规格名称重复');
            }
            if(false !== $spec_item->allowField(true)->validate(true)->save($post_data,['id' => $id])){
                // 更新所有规格缓存
                $MallSpecCatsModel = new MallSpecCatsModel;
                $MallSpecCatsModel->cache();
                $this->success('商品规格修改成功','index');
            }else{
                $this->error($spec_item->getError());
            }
        }
        // 获取规格组数组
        $spec_groups = [];
        foreach (MallSpecGroupsModel::all() as $key => $group) {
             $spec_groups[$group->id] = $group->getData();
        }
        $this->assign([
            'submenu'       => $this->submenu,
            'spec_groups'   => $spec_groups,
            'spec_cats'     => MallSpecCatsModel::all(),
            'data'          => $spec_item->getData(),
            'parent_spec_group' => $spec_group->getData()
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格删除
     * @access public
     * @return void
     */
    public function del($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少商品规格id');
        }
        $spec_item = MallSpecItemsModel::get($id);
        if(!$spec_item){
            $this->error('需要删除的商品规格不存在');
        }
        // 删除规格
        $spec_item->delete();
        // 更新所有规格缓存
        $MallSpecCatsModel = new MallSpecCatsModel;
        $MallSpecCatsModel->cache();
        $this->success('商品规格删除成功', 'index');
    }
}